﻿/*
Mouse SKIN.

Cursor In "THIS AREA" can be control :
並方便綁定動畫到 Mouse 套用 up/normal, over, out, down

RELEASE();	// clear up

*/
package com.cf.core.model{
	import flash.ui.Mouse;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.Sprite;
	import com.cf.core.model.MouseSkin;
	import com.cf.core.Utils.tools;
	public class MouseBase extends Sprite{
		private var G_skin:MouseSkin;
		public function set SKIN(_s:MouseSkin):void{G_skin=_s; dressSkin();}
		public function get SKIN():MouseSkin{return G_skin}
		private var G_parent:*;
		
		public function MouseBase(){
			mouseEnabled=false;
			mouseChildren=false;
		}//fn
		
		public function set ASSIGN(_obj:*):void{
			G_parent=_obj;
			G_parent.addEventListener(MouseEvent.MOUSE_MOVE, handlerMouse);
			G_parent.addEventListener(MouseEvent.MOUSE_DOWN, handlerMouse);
			G_parent.addEventListener(MouseEvent.MOUSE_OUT, handlerMouse);
			G_parent.addEventListener(Event.MOUSE_LEAVE, handlerHide);
			G_parent.addChild(this);
			G_parent.addEventListener(Event.REMOVED_FROM_STAGE, RELEASE);
		}//fn
		
		public function dressSkin():void{
			tools.emptySprite(this);
			visible=false;
			this.addChild(G_skin);
			Mouse.hide();
		}//fn
		
		public function reset():void{
			tools.emptySprite(this);
			Mouse.show();
		}//fn
		
		public function RELEASE(e:Event=null):void{
			reset();
			try{
				G_parent.removeEventListener(Event.REMOVED_FROM_STAGE, RELEASE);
				G_parent.removeEventListener(MouseEvent.MOUSE_MOVE, handlerMouse);
				G_parent.removeEventListener(MouseEvent.MOUSE_DOWN, handlerMouse);
				G_parent.removeEventListener(MouseEvent.MOUSE_OUT, handlerMouse);
				G_parent.removeEventListener(Event.MOUSE_LEAVE, handlerHide);
				trace("MouseBase : RELEASE finish");
			}catch(e){}
		}//fn
		
		private function handlerMouse(e:MouseEvent):void{
			switch(e.type){
				case MouseEvent.MOUSE_OVER:
				case MouseEvent.MOUSE_MOVE:
					// trace("x="+e.localX+", y="+e.localY);
					visible = true;
					this.x = e.stageX;
					this.y = e.stageY;
					e.updateAfterEvent();	// More smooth.
				break;
				case MouseEvent.MOUSE_DOWN:
					//trace("MouseBase : "+ e.target +" id:"+ e.target.name);
					try{
						G_skin.ani("down");
					}catch(e){}
				break;
				case MouseEvent.MOUSE_OUT: break;
			}//case
		}//fn
		private function handlerHide(e:*=null):void{
			// When the Mouse Leave scene, hide the skin.
			visible = false;
			trace("LEAVE");
		}//fn
	}//class
}//package